<?php
header('Content-type:text/html;charset=utf-8');

//包含数据库配置
include '../../public/peizhi.php';

//包含函数
include '../../public/func.php';

//接值
$username = $_POST['username'];
$pass = $_POST['pass'];
$repass = $_POST['repass'];
$qx = $_POST['qx'];

//判断用户名长度
$strlen = strlen($username);

if($strlen <= 0 || $strlen > 10){
	exit('<script>alert("没有输入用户名或用户名超过10个字");window.location.href="add.php";</script>');
}

//判断密码是否一致
if(strlen($pass) >= 6){
	if($pass != $repass){
		exit('<script>alert("密码不一致");window.location.href="add.php";</script>');
	}else{
		$newpass = md5($pass);
	}
}else{
	exit('<script>alert("密码长度最少6位");window.location.href="add.php";</script>');
}

//获取注册时间
$zctime = time();

//获取用户ip
$uid = $_SERVER['REMOTE_ADDR'];

//判断IP格式
if($uid == '::1'){
	$newuid = ip2long('127.0.0.1');
}else{
	$newuid = ip2long($uid);
}

//调用函数连接数据库
$link = db();

//准备SQL语句
$sql = "insert into user(name,pass,qx,zctime,zcip) values('{$username}','{$newpass}','{$qx}','{$zctime}','{$newuid}')";

//执行SQL语句
if(mysqli_query($link,$sql)){
	echo '<script>alert("添加用户成功");window.location.href="show.php";</script>';
}else{
	echo '<script>alert("添加用户失败");window.location.href="add.php";</script>';
}

//关闭数据库连接
mysqli_close($link);

